//
//  SZFilmCriticController.m
//  KaiJing
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "SZFilmCriticController.h"
#import "PKRequestManager.h"
#import "CJFilmCriticModel.h"
#import "CJFilmCriticTableViewCell.h"
#import "CJPictorialCellWebViewViewController.h"
#import "UMSocial.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "SZBaseNavController.h"
@interface SZFilmCriticController ()

@property (nonatomic, strong) NSMutableArray *sourceArray;
@property (nonatomic, assign) NSInteger page;    //刷新
@property (nonatomic, strong) MBProgressHUD *hud;
@end

@implementation SZFilmCriticController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    

    _sourceArray = [NSMutableArray array];

    [self requestData];   //请求数据
    [self hudMore];


    //刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

   
        _page = 1;
        [self requestData];
    
    }];

    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page += 1;
        [self requestData];
    }];
}


//菊花
-(void)hudMore
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.labelText = @"休息一下";
    _hud.minSize = CGSizeMake(100, 100);
    _hud.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_hud];
    [_hud show:YES];
}


- (void)requestData {
    
    NSString *urlStr = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=acinecism&androidflag=1&appfrom=ios&iosversion=3.13&page=%ld",_page];
    
    [PKRequestManager requestWithUrlString:urlStr parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
        [_hud hide:YES];

        if (_page == 1) {
            [_sourceArray removeAllObjects];
        }
        
        
        NSArray *array = dataDic[@"data"][@"list"];
        for (NSDictionary *dic in array) {
            CJFilmCriticModel *model = [[CJFilmCriticModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_sourceArray addObject:model];
        }
        
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
    
    
    
    
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _sourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *idenfitier = @"CJFilmCriticTableViewCell";
    CJFilmCriticTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfitier];
    if (cell == nil) {
        cell = [[CJFilmCriticTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:idenfitier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CJFilmCriticModel *model = _sourceArray[indexPath.row];
    cell.model = model;
    
    //NSInteger tid = [model.tid integerValue];
    //点赞
    cell.bottomView.praiseButton.tag = 5000 + indexPath.row;
    [cell.bottomView.praiseButton addTarget:self action:@selector(praiseButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    cell.bottomView.moreButton.tag = 6000 + indexPath.row;
    [cell.bottomView.moreButton addTarget:self action:@selector(moreButton:) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJFilmCriticModel *model = _sourceArray[indexPath.row];
    
    self.filmBlock(model.tid);
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CJFilmCriticModel *model = _sourceArray[indexPath.row];
    
    CGFloat height = [CJFilmCriticTableViewCell imageVHeightForModel:model];
    
    return height + 170;
    
    
}

//点赞
- (void)praiseButton:(UIButton *)button {
    
    
    // 是否登录userName isEqualToString:@"YES"]
    if ([SZLoginTool shareLogin].isLogin) {
        
        button.selected = !button.selected;
        
        NSString *isLike = nil;
        if (button.selected) {
            isLike = @"like";
        } else {
            isLike = @"unlike";
        }
        
        
        CJFilmCriticModel *model = _sourceArray[button.tag - 5000];
        
        NSString *str = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=threadlikecp&op=%@&tid=%@&formhash=%@&androidflag=1&appfrom=ios&iosversion=3.13",isLike,model.tid, [SZLoginTool shareLogin].formhash];
        
        [PKRequestManager requestWithUrlString:str parDic:nil method:GET finish:^(NSDictionary *dataDic) {
            if (dataDic[@"data"][@"result"]) {
                SZLog(@"点赞成功");
                
                model.isliked = button.selected;
            } else {
                SZLog(@"点赞失败");
                button.selected = !button.selected;
            }
            
            
        } error:^(NSError *error) {
            button.selected = !button.selected;
        }];
        
    } else {
        
        SZLoginViewController *loginVC = [[SZLoginViewController alloc] init];
        SZBaseNavController *loginNav = [[SZBaseNavController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNav animated:YES completion:nil];
        
        
    }
}

//分享
- (void)moreButton:(UIButton *)button {
    
    CJPictorialMainModel *model = _sourceArray[button.tag - 6000];
    NSString *url = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=viewthread&tid=%@&isfrommobile=1",model.tid];
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *viewController = window.rootViewController;
    [UMSocialSnsService presentSnsIconSheetView:viewController
                                         appKey:@"569ee21fe0f55a76b10021e3"
                                      shareText:[NSString stringWithFormat:@"<<%@>>详情点击%@",model.subject,url]
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToEmail,UMShareToWechatFavorite,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToTencent,nil]
                                       delegate:nil];
    
}










@end
